package com.gzc.pm.util
{
	import cn.skyclass.business.SessionManager;
	
	import com.gzc.pm.bean.AlarmRuleBean;
	import com.gzc.pm.bean.DetectioncardBean;
	import com.gzc.pm.bean.DetsetbBean;
	import com.gzc.pm.bean.LineChartItemBean;
	import com.gzc.pm.bean.ToleranceBean;
	import com.gzc.pm.enum.StandardEnum;
	import com.gzc.pm.logic.ToleranceLoader;
	
	import mx.collections.ArrayCollection;
	import mx.core.SoundAsset;

	public class MyUtil
	{
		public function MyUtil()
		{
		}
		
		[Embed('assets/pm/sound/alarm.mp3')]  
		private static var alarm_mp3:Class;  
		
		public static var alarm:SoundAsset = new alarm_mp3() as SoundAsset; 
		
		public static function date2String(date:Date):String{
			var y:String = String(date.fullYear);
			var M:String = date.month<9?'0'+String(date.month+1):String(date.month+1);
			var d:String = date.date<10?'0'+String(date.date):String(date.date);
			var h:String = date.hours<10?'0'+String(date.hours):String(date.hours);
			var m:String = date.minutes<10?'0'+String(date.minutes):String(date.minutes);
			var s:String = date.seconds<10?'0'+String(date.seconds):String(date.seconds);
			
			return y+M+d+h+m+s;
		}
		
		public static function date2StringWithoutHMS(date:Date, begin:Boolean):String{
			var y:String = String(date.fullYear);
			var M:String = date.month<9?'0'+String(date.month+1):String(date.month+1);
			var d:String = date.date<10?'0'+String(date.date):String(date.date);
			var h:String = begin ? '00' : '23';
			var m:String = begin ? '00' : '59';
			var s:String = begin ? '00' : '59';
			
			return y+M+d+h+m+s;
		}
		
		/**
		 * 计算平均值和标准差
		 */
		public function calcCollectionsAvgAndStdD(ds:ArrayCollection, key:String, withStdD:Boolean=false):Object{
			if(!ds || ds.length==0) return {avg:0, stdD:0, ucl:0, lcl:0};
			
			var len:int = ds.length;
			if(ds.length==1){
				var bean:DetectioncardBean = ds.getItemAt(0) as DetectioncardBean;
				var bv:Number = Number(bean[key]);
				return {avg:bv, stdD:bv, ucl:bv, lcl:bv};
			}
			
			var avg:Number = 0;
			for each(var collc:DetectioncardBean in ds){
				avg += Number(collc[key]);
			}
			avg /= len;
			
			var stdD:Number = 0;
			var ucl:Number = 0;
			var lcl:Number = 0;
			if(withStdD){
				var D:Number = 0;
				for each(var collc1:DetectioncardBean in ds){
					var v:Number = Number(collc1[key]);
					D += Math.pow((v-avg), 2);
				}
//				D /= len-1;
				stdD = Math.sqrt(D/(len-1));
				ucl = avg+stdD*3;
				lcl = avg-stdD*3;
			}
			
			return {avg:Number(avg.toFixed(3)), stdD:stdD, ucl:Number(ucl.toFixed(3)), lcl:Number(lcl.toFixed(3))};
		}
		
		/**
		 * 按控制图告警规则，对lineChartItem列表进行判断
		 */
		public function checkByControlRules(chartItemList:ArrayCollection):void{
			if(!chartItemList || chartItemList.length==0) return;
			
			var rule:AlarmRuleBean = SessionManager.current.getAttribute('alarmRule') as AlarmRuleBean;
			//rule2
			var greaterOrLowerThanAvgValue:int = NaN; //1：大于中心线，0：等于中心线，-1：小于中心线
			var greaterOrLowerThanAvgCount:uint = 1;//连续次数
			//rule3
			var greaterOrLowerThanPreValue3:int = NaN; //1：大于前一个值，0：等于前一个值，-1：小于前一个值
			var greaterOrLowerThanPreCount:uint = 1;//连续次数
			//rule4
			var greaterOrLowerThanPreValue4:int = NaN; //1：大于前一个值，0：等于前一个值，-1：小于前一个值
			var upAndDownCount:uint = 2;//连续次数
			
			var len:uint = chartItemList.length;
			for(var i:uint = 0; i<len; i++){
				var curBean:LineChartItemBean = chartItemList.getItemAt(i) as LineChartItemBean;
				if(rule.bRule1 && checkByRule1(curBean)) curBean.b1 = true;
				if(rule.bRule2 && checkByRule2(i, chartItemList)) curBean.b2 = true;
				if(rule.bRule3 && checkByRule3(i, chartItemList)) curBean.b3 = true;
				if(rule.bRule4 && checkByRule4(i, chartItemList)) curBean.b4 = true;
				if(rule.bRule5 && checkByRule5(i, chartItemList)) curBean.b5 = true;
				if(rule.bRule6 && checkByRule6(i, chartItemList)) curBean.b6 = true;
				if(rule.bRule7 && checkByRule7(i, chartItemList)) curBean.b7 = true;
				if(rule.bRule8 && checkByRule8(i, chartItemList)) curBean.b8 = true;
			}
			
			function checkByRule1(bean:LineChartItemBean):Boolean{
				return Math.abs(bean.value-bean.average) > bean.stdD*rule.vRule1;
			}
			function checkByRule2(index:uint, chartItemList:ArrayCollection):Boolean{
				if(index >= rule.vRule2-1){
					var positiveCount:uint = 0; //大于平均值的数量
					var negativeCount:uint = 0; //小于平均值的数量
					for(var sub:uint=0; sub<=rule.vRule2-1; sub++){
						var item:LineChartItemBean = chartItemList.getItemAt(index-sub) as LineChartItemBean;
						if(item.value-item.average > 0) positiveCount++;
						if(item.value-item.average < 0) negativeCount++;
						if(item.value-item.average == 0) return false;
						if(positiveCount*negativeCount != 0) return false;
					}
					return true;
				}
				return false;
			}
			function checkByRule3(index:uint, chartItemList:ArrayCollection):Boolean{
				if(index >= rule.vRule3){
					var positiveCount:uint = 0; //递增数量
					var negativeCount:uint = 0; //递减数量
					for(var sub:uint=0; sub<=rule.vRule3-1; sub++){
						var item:LineChartItemBean = chartItemList.getItemAt(index-sub) as LineChartItemBean;
						var preItem:LineChartItemBean = chartItemList.getItemAt(index-sub-1) as LineChartItemBean;
						if(item.value-preItem.value > 0) positiveCount++;
						if(item.value-preItem.value < 0) negativeCount++;
						if(item.value-preItem.value == 0) return false;
						if(positiveCount*negativeCount != 0) return false;
					}
					return true;
				}
				return false;
			}
			function checkByRule4(index:uint, chartItemList:ArrayCollection):Boolean{
				if(index >= rule.vRule4-1){
					var status:uint = NaN; //1:递增 -1:递减 0:相等
					for(var sub:uint=0; sub<=rule.vRule4-2; sub++){
						var item:LineChartItemBean = chartItemList.getItemAt(index-sub) as LineChartItemBean;
						var preItem:LineChartItemBean = chartItemList.getItemAt(index-sub-1) as LineChartItemBean;
						var curStatus:uint = (item.value-preItem.value>0) ? 1 : (item.value-preItem.value<0 ? -1 : 0);
						if(curStatus == 0) return false;
						if(isNaN(status)) {
							status = curStatus;
							continue;
						}else{
							if(curStatus*status > 0) return false;
							status = curStatus;
						}
					}
					return true;
				}
				return false;
			}
			function checkByRule5(index:uint, chartItemList:ArrayCollection):Boolean{
				if(index >= rule.vRule5){
					var positiveCount:uint = 0; //大于2个标准差的数量
					var negativeCount:uint = 0; //小于2个标准差的数量
					for(var sub:uint=0; sub<=rule.vRule5; sub++){
						var item:LineChartItemBean = chartItemList.getItemAt(index-sub) as LineChartItemBean;
						if(item.value-item.average > item.stdD*2) positiveCount++;
						if(item.value-item.average < -item.stdD*2) negativeCount++;
						if(positiveCount>=rule.vRule5 || negativeCount>=rule.vRule5) return true;
					}
				}
				return false;
			}
			function checkByRule6(index:uint, chartItemList:ArrayCollection):Boolean{
				if(index >= rule.vRule6){
					var positiveCount:uint = 0; //大于1个标准差的数量
					var negativeCount:uint = 0; //小于1个标准差的数量
					for(var sub:uint=0; sub<=rule.vRule6; sub++){
						var item:LineChartItemBean = chartItemList.getItemAt(index-sub) as LineChartItemBean;
						if(item.value-item.average > item.stdD) positiveCount++;
						if(item.value-item.average < -item.stdD) negativeCount++;
						if(positiveCount>=rule.vRule6 || negativeCount>=rule.vRule6) return true;
					}
				}
				return false;
			}
			function checkByRule7(index:uint, chartItemList:ArrayCollection):Boolean{
				if(index >= rule.vRule7-1){
					var count:uint = 0;
					for(var sub:uint=0; sub<=rule.vRule7-1; sub++){
						var item:LineChartItemBean = chartItemList.getItemAt(index-sub) as LineChartItemBean;
						if(Math.abs(item.value-item.average) >= item.stdD) return false;//不在1个标准差以内
					}
					return true;
				}
				return false;
			}
			function checkByRule8(index:uint, chartItemList:ArrayCollection):Boolean{
				if(index >= rule.vRule8-1){
					var count:uint = 0;
					for(var sub:uint=0; sub<=rule.vRule8-1; sub++){
						var item:LineChartItemBean = chartItemList.getItemAt(index-sub) as LineChartItemBean;
						if(Math.abs(item.value-item.average) <= item.stdD) return false;//不大于1个标准差
					}
					return true;
				}
				return false;
			}
		}
		
		/**
		 * 按公差图告警规则，对lineChartItem列表进行判断
		 */
		public function checkByToleranceRules(chartItemList:ArrayCollection):void{
			var len:uint = chartItemList.length;
			for(var i:uint = 0; i<len; i++){
				var curBean:LineChartItemBean = chartItemList.getItemAt(i) as LineChartItemBean;
				curBean.b1_t = (curBean.value>curBean.level1);
				curBean.b2_t = (curBean.value<curBean.level2);
			}
		}
		
		/**
		 * 检查是否有新的告警，有则播放报警声
		 */
		public static function checkNewAlarm(chartItemList:ArrayCollection, lastLength:uint):Boolean{
			if(!chartItemList || chartItemList.length==0) return false;
			for(var i:uint=lastLength; i<chartItemList.length; i++){
				var chartItem:LineChartItemBean = LineChartItemBean(chartItemList.getItemAt(i));
				if(chartItem.b1 || chartItem.b2 || chartItem.b3 || chartItem.b4 ||
					chartItem.b5 || chartItem.b6 || chartItem.b7 || chartItem.b8){
					return true;
				}
			}
			return false;
		}
		
	}
}